package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.DishFlavor;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.service.DishFlavorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hebing
 * @version 1.0
 */
@Service
public class DishFlavorServiceImpl implements DishFlavorService {
    @Autowired
    private DishFlavorMapper dfm;

    /**
     * 添加数据到flavor表
     *
     * @param flavors
     * @return
     */
    @Override
    public void addDishFlavor(List<DishFlavor> flavors) {
        //对集合数据进行遍历
        for (DishFlavor flavor : flavors) {
            dfm.insert(flavor);
        }
    }

    /**
     * 根据dishId查询口味列表
     *
     * @param dishId
     * @return
     */
    @Override
    public List<DishFlavor> selectFlavors(Long dishId) {
        //设置查询条件
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishId);
        return dfm.selectList(lqw);
    }

    /**
     * 修改口味表
     *
     * @param flavorList
     * @return
     */
    @Override
    public int update(List<DishFlavor> flavorList) {
        //做修改
        if(flavorList!=null){
            for (DishFlavor dishFlavor : flavorList) {
                //判断之后添加数据
                int rows = dfm.insert(dishFlavor);
                return rows;
            }
        }
        return flavorList.size();
    }

    /**
     * 根据id删除
     *
     * @param flavorList
     * @return
     */
    @Override
    public int delete(List<DishFlavor> flavorList) {
        //构建条件对象
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<>();
        if(flavorList!=null){
            for (DishFlavor dishFlavor : flavorList) {
                lqw.eq(DishFlavor::getDishId,dishFlavor.getDishId());
                int rows1 = dfm.delete(lqw);
                return rows1;
            }
        }
        return flavorList.size();
    }

}
